#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Facebook, Inc. and its affiliates.
import os
from pathlib import Path
import json
import numpy as np
import tqdm
from PIL import Image
import cv2
from panopticapi.utils import IdGenerator, save_json
import pycocotools.mask as mask_util
import glob

data_dir = Path('/root/ws/data/whu/')

def copy2raw_files():
    src_dir = data_dir / "train" / "image"
    dest_dir = data_dir / "train" / "image"
    
    dest_dir = data_dir / "processed_dir"
    
    index = 0
    for file in tqdm.tqdm(list(src_masks_dir.iterdir())):
        
def handle_train_jsonfile(jsonfile):
    dest_ann_file = data_dir / "processed_dir" / jsonfile
    anndir = data_dir / "annotations"
    annfile = anndir / jsonfile
    with open(annfile) as f:
        dataall = json.load(f)
        annotations = dataall["annotations"]
        for ann in annotations:
            ann["ins_id"] = ann["id"] 

        images = dataall["images"]
        for img in images:
            file_name = "train_" + img["file_name"]
            imgfile = 
            
            img["file_name"] = "train_" + img["file_name"] 
        save_json(dataall,dest_ann_file)
def handle_val_jsonfile(jsonfile):
    dest_ann_file = data_dir / "processed_dir" / jsonfile
    anndir = data_dir / "annotations"
    annfile = anndir / jsonfile
    with open(annfile) as f:
        dataall = json.load(f)
        annotations = dataall["annotations"]
        for ann in annotations:
            ann["ins_id"] = ann["id"] 

        images = dataall["images"]
        for img in images:
            fname = img["file_name"]
            img["file_name"] = fname.replace("val_","val")
        save_json(dataall,dest_ann_file)
if __name__ == "__main__":
    # handle_val_jsonfile("WHU_building_val.json")
    handle_train_jsonfile("WHU_building_train.json")
    #handle_val_jsonfile("WHU_building_test.json")
    
    